﻿using AutoMapper;
using BaBaoFan.Modifier.Model;
using BaBaoFan.Modifier.Models;
using BaBaoFan.Modifier.Service;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Caching.Memory;
using System.Linq;

namespace BaBaoFan.Modifier.Controllers
{
    public class AlgorithmInfoController : BaseController
    {

        private readonly IAlgorithmInfoService _algorithmInfoService;
        private readonly IAlgorithmService _algorithmService;
        private readonly IMillInfoService _millInfoService;
        private readonly IMapper _mapper;
        public AlgorithmInfoController(
            IAlgorithmInfoService algorithmInfoService,
            IAlgorithmService algorithmService,
            IMillInfoService millInfoService,
            IMapper mapper,
            IMemoryCache memoryCache) : base(memoryCache)
        {
            _algorithmInfoService = algorithmInfoService;
            _algorithmService = algorithmService;
            _millInfoService = millInfoService;
            _mapper = mapper;
        }
        public IActionResult List()
        {
            var algorithmInfos = _algorithmInfoService.GetAlgorithmInfos();
            return View(algorithmInfos);
        }
        public IActionResult Delete(int[] ids)
        {
            _algorithmInfoService.Delete(ids);
            return Json("ok");
        }
        [HttpGet]
        public IActionResult Add()
        {
            AlgorithmInfoCreateOrUpdateViewModel vm = new AlgorithmInfoCreateOrUpdateViewModel
            {
                AvaliableAlgorithms = _algorithmService.GetCoinAlgorithms().Select(x => new SelectListItem { Text = x.Algorithm, Value = x.Id.ToString() }),
                AvaliableMillInfos = _millInfoService.GetMillInfos().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }),
                AvaliableAlgorithmUnits = _algorithmService.GetAlgorithmUnits().Select(x => new SelectListItem { Text = x.Unit, Value = x.Id.ToString() })
            };
            return View(vm);
        }
        [HttpPost]
        public IActionResult Add(AlgorithmInfoCreateOrUpdateViewModel vm)
        {
            var model = _mapper.Map<AlgorithmInfoEntity>(vm);
            _algorithmInfoService.Add(model);
            return Redirect("/AlgorithmInfo/List");
        }
        [HttpGet]
        public IActionResult Edit(int id)
        {
            var algorithmInfo = _algorithmInfoService.GetById(id);
            var vm = _mapper.Map<AlgorithmInfoCreateOrUpdateViewModel>(algorithmInfo);
            vm.AvaliableAlgorithms = _algorithmService.GetCoinAlgorithms().Select(x => new SelectListItem { Text = x.Algorithm, Value = x.Id.ToString() });
            vm.AvaliableMillInfos = _millInfoService.GetMillInfos().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() });
            vm.AvaliableAlgorithmUnits = _algorithmService.GetAlgorithmUnits().Select(x => new SelectListItem { Text = x.Unit, Value = x.Id.ToString() });
            return View(vm);
        }
        [HttpPost]
        public IActionResult Update(AlgorithmInfoCreateOrUpdateViewModel vm)
        {
            var model = _mapper.Map<AlgorithmInfoEntity>(vm);
            _algorithmInfoService.Update(model);
            return Redirect("/AlgorithmInfo/List");
        }
        [HttpPost]
        public IActionResult Search(string algorithm, string millName, int page = 1, int limit = 10)
        {
            var infoCondition = new AlgorithmInfoCondition
            {
                Algorithm = algorithm,
                MillName = millName,
                PageIndex = page,
                PageSize = limit,
                TotalCount = 0
            };
            var algorithmInfos = _algorithmInfoService.Search(infoCondition);
            return Json(new { code = 0, count = infoCondition.TotalCount, msg = "", data = algorithmInfos });
        }
    }
}